grammar SZCGrammar;

WS: [ \t\r\n]+ -> skip;

IDENTIFIER: ([a-zA-Z_][a-zA-Z0-9_]*) | [\u4e00-\u9fa5]+;

LINE_COMMENT: '#' .*? '\r'? '\n' -> skip;
COMMIT: '/*' .*? '*/' -> skip;

prog: (funcDef)*;

abstractStatement: returnStatement | varDeclStatement | varDefStatement |
                    blockStatement | ifElseStatement | whileStatement | breakStatement |
                    continueStatement;

returnStatement: '求得为' exp;
blockStatement: block;

funcDef: '演绎' funcName '(' ')' block ;

funcName: IDENTIFIER;

block: '{' (abstractStatement)* '}';

// 一元表达式
exp: lOrExp;
primaryExp: '(' exp ')' | NUMBER | lval;
unaryExp: primaryExp | unaryOp unaryExp;
unaryOp: '+' | '-' | '!';
mulExp: unaryExp | mulExp '*' unaryExp | mulExp '/' unaryExp;
addExp: mulExp | addExp '+' mulExp | addExp '-' mulExp;
relExp: addExp | relExp '>' addExp | relExp '<' addExp | relExp '<=' addExp | relExp '>=' addExp;
eqExp: relExp | eqExp '==' relExp | eqExp '!=' relExp;
lAndExp: eqExp | lAndExp '&&' eqExp;
lOrExp: lAndExp | lOrExp '||' lAndExp;

// 变量
lval: IDENTIFIER;
initVal: exp;
varDef: IDENTIFIER | IDENTIFIER '=' initVal ;
varDefStatement: varDef;
varDeclStatement: '令' varDef;

// 条件分支
conditionCombine: exp | conditionCombine '且' exp |  conditionCombine '或' exp;
ifElseStatement: '若' conditionCombine abstractStatement '否则' abstractStatement | '若' conditionCombine abstractStatement;

// 循环
whileStatement: '复若' conditionCombine abstractStatement;
breakStatement: '弃之';
continueStatement: '续之';

NUMBER: [0-9_]+;